/**
 * 数据类型判断工具类
 */


import {getItem} from "../services/LocalDataService";
import Config from "../Config";

/**
 *判断是否是字符串
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isString: Function = (obj: any): boolean => {
    return typeof (obj) === 'string';
};

/**
 *判断是否是数字
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isNumber: Function = (obj: any): boolean => {
    return typeof (obj) === 'number';
};

/**
 * 判断是否是方法
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isFunction: Function = (obj: any): boolean => {
    return typeof (obj) === 'function';
};
/**
 * 判断是都是boolean类型
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isBoolean: Function = (obj: any): boolean => {
    return typeof (obj) === 'boolean';
};

/**
 * 判断是否是数组
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isArray: Function = (obj: any): boolean => {
    return obj instanceof Array;
};

/**
 * 判断是否是数组
 * @param obj
 * @returns {boolean}
 */

// @ts-ignore
const isObject: Function = (obj: any): boolean => {
    return Object.prototype.toString.call(obj) === '[object Object]';
};

// @ts-ignore
const isJson: Function = (obj: any): boolean => {
    try {
        let jsonObj: any = JSON.parse(obj);
        return jsonObj;
    } catch (error) {
        // 解析失败，变量不是JSON
        return false;
    }
};

/**
 * 判断是否是一个FormData对象
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isFormData: Function = (obj: any): boolean => {
    return Object.prototype.toString.call(obj) === '[object FormData]';
};

/**
 * 判断是否为空
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isBlank: Function = (obj: any): boolean => {
    return obj === null || obj === undefined || obj === "";
};

/**
 * 判断是都是一个空对象
 * @param obj
 * @returns {boolean}
 */
// @ts-ignore
const isEmptyObj: Function = (obj: any): boolean => {
    for (let key in obj) {
        // eslint-disable-next-line no-prototype-builtins
        if (obj.hasOwnProperty(key)) {
            return false
        }
    }
    return true;
};

export {
    isString,
    isNumber,
    isFunction,
    isBoolean,
    isArray,
    isObject,
    isFormData,
    isBlank,
    isJson,
    isEmptyObj
};